var moment = require('moment');
var tcpp = require('tcp-ping');

var hosts = [];

var cfg = {
    timeout: 1000
};

var getHosts = function () {
    return hosts;
};

var removeHost = function (domain) {
    hosts.forEach(function (host, index, hostsArray) {
        if (domain === host.domain) {
            hostsArray.splice(index);
        }
    });
};

var updateHost = function (domain, port) {
    var exists = false;
    hosts.forEach(function (host, index, hostsArray) {
        if (domain === host.domain) {
            exists = true;
            hostsArray.splice(index);
        }
    });
    if (exists) {
        hosts.push({
            domain: domain,
            port: port
        });
        return true;
    } else {
        return false;
    }
};

var addHost = function (domain, port) {
    var exists = false;
    hosts.forEach(function (host, index, hostsArray) {
        if (domain === host.domain) {
            exists = true;
        }
    });
    if (!exists) {
        hosts.push({
            domain: domain,
            port: port
        });
        return true;
    } else {
        return false;
    }
};

var pingHost = function (domain) {
    hosts.forEach(function (host, index, hostsArray) {
        if (domain === host.domain) {
            var port = host.port ? host.port : 80;
            tcpp.ping({
                address: domain,
                port: port,
                timeout: 1000,
                attempts: 1
            }, function (err, result) {
                if (!host.resultList) {
                    host.resultList = [];
                }
                var timestamp = moment();
                var date = moment(timestamp).format('YYYY-MM-DD HH:mm:ss.SSS');
                if (!err) {
                    host.resultList.push({
                        timestamp: timestamp,
                        date: date,
                        reachable: true,
                        delay: result.max
                    });
                } else {
                    host.resultList.push({
                        timestamp: timestamp,
                        date: date,
                        reachable: false
                    });
                }
                var maxLen = 3;
                host.resultList = host.resultList.slice(0 - maxLen);

                var maxDelay, minDelay, totalDelay = 0, reachableCount = 0;
                host.resultList.forEach(function (result, index, resultArray) {
                    if (result.reachable) {
                        reachableCount = reachableCount + 1;

                        var delay = result.delay;
                        if (!maxDelay || delay > maxDelay) {
                            maxDelay = delay;
                        }
                        if (!minDelay || delay < minDelay) {
                            minDelay = delay;
                        }
                        totalDelay = totalDelay + delay;
                    }
                    host.maxDelay = maxDelay;
                    host.avgDelay = totalDelay / reachableCount;
                    host.minDelay = minDelay;
                    host.rate = reachableCount / maxLen;
                });
            });
        }
    });
};

var pingAllHosts = function () {
    hosts.forEach(function (host, index, hostsArray) {
        pingHost(host.domain);
    });
};

module.exports.getHosts = getHosts;
module.exports.addHost = addHost;
module.exports.removeHost = removeHost;
module.exports.updateHost = updateHost;
// module.exports.pingHost = pingHost;
module.exports.pingAllHosts = pingAllHosts;

